<?php 
namespace app\index\controller;
use think\Controller;
use think\Db;//导入数据库类 (DB类)
use think\Session;
use think\Request;


class Forgot extends Controller
{
    public function forgota()
    {
        return view('/forgot/forgota');
    }
    public function forgotaa()
    {
        $list = db('user')->where('tel',input('num'))->find();
        if($list){
            Session::set('forgot',$list);
            return 1;
        }else{
            return 'no';
        }
        
    }
    public function forgotb()
    {
        $tel = db('user')->where('id',input('id'))->find();

        $this->assign('tel',$tel['tel']);
        return view('/forgot/forgotb');
    }
    public function forgotc()
    {
        return view('/forgot/forgotc');
    }
    public function forgotcc()
    {
        // dump(input('session.forgot.id'));
        if(Request::instance()->isPost()){
            // 先进行判断新密码和旧密码是否一致,如果一致返回修改成功
            $old = db('user')->where('id',input('session.forgot.id'))->find();
            $pwd = input('new_password');
            if($old['pwd'] == $pwd){
                return $this->redirect('forgotd');
                exit;
            }
            $update = db('user')->where('id',input('session.forgot.id'))->update(['pwd'=>$pwd]);
            if($update){
                return $this->redirect('forgotd');
            }else{
                return $this->error('修改失败');
            }
        }
    }
    public function forgotd()
    {
        return view('/forgot/forgotd');
    }
    // 发送验证码
    public function send()
    {
        $code = mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9);
        Session::set('code',$code);

        $phone = input('post.tel');
        $rec = sendTemplateSMS($phone,array($code,'5'),'1');
                if($rec['status'] !=0 ){
                    return 'no';
                }else{
                    return $code;
                }

    }
    // 判断验证码是否正确
    public function doSend()
    {
        if(Request::instance()->isPost()){
            if(input('yzm') != Session::get('code')){
                return 'no';
            }else{
                return 'ok';
            }
        }
    }
  

}